```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tomcat请求限流与流量控制方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f9fafb;
            color: #1f2937;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            color: #f8fafc;
            border-radius: 0.5rem;
            overflow-x: auto;
        }
        .method-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 40%;
            background-color: #e0e7ff;
            z-index: -1;
            opacity: 0.7;
            border-radius: 0.125rem;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl lg:text-6xl font-bold mb-6">Tomcat 请求限流与流量控制</h1>
            <p class="text-xl md:text-2xl text-indigo-100 max-w-3xl mx-auto mb-8">保护服务器资源，防止过载，确保系统稳定运行的高级策略</p>
            <div class="flex justify-center space-x-4">
                <a href="#methods" class="bg-white text-indigo-600 px-6 py-3 rounded-lg font-medium hover:bg-indigo-50 transition duration-300">
                    <i class="fas fa-code mr-2"></i>查看实现方案
                </a>
                <a href="#visualization" class="bg-indigo-800 text-white px-6 py-3 rounded-lg font-medium hover:bg-indigo-700 transition duration-300">
                    <i class="fas fa-project-diagram mr-2"></i>方案对比
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-8 sm:p-10">
                <h2 class="text-3xl font-bold text-gray-900 mb-6">为什么需要限流与流量控制？</h2>
                <p class="text-lg text-gray-700 leading-relaxed mb-6">
                    在高并发场景下，Tomcat 服务器可能会面临请求过载的风险。有效的限流和流量控制策略可以：
                </p>
                <ul class="grid md:grid-cols-3 gap-6 mb-8">
                    <li class="bg-indigo-50 p-4 rounded-lg">
                        <i class="fas fa-shield-alt text-indigo-600 text-2xl mb-3"></i>
                        <h3 class="font-semibold text-lg mb-2">保护服务器资源</h3>
                        <p class="text-gray-700">防止单一用户或恶意攻击消耗过多资源</p>
                    </li>
                    <li class="bg-indigo-50 p-4 rounded-lg">
                        <i class="fas fa-balance-scale text-indigo-600 text-2xl mb-3"></i>
                        <h3 class="font-semibold text-lg mb-2">确保公平性</h3>
                        <p class="text-gray-700">合理分配资源给所有用户</p>
                    </li>
                    <li class="bg-indigo-50 p-4 rounded-lg">
                        <i class="fas fa-rocket text-indigo-600 text-2xl mb-3"></i>
                        <h3 class="font-semibold text-lg mb-2">提升系统稳定性</h3>
                        <p class="text-gray-700">避免过载导致的系统崩溃</p>
                    </li>
                </ul>
            </div>
        </section>

        <!-- Methods Section -->
        <section id="methods" class="mb-16">
            <h2 class="text-3xl font-bold text-gray-900 mb-8 flex items-center">
                <i class="fas fa-tools mr-3 text-indigo-600"></i>实现方案
            </h2>

            <!-- Method 1 -->
            <div class="grid md:grid-cols-1 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md overflow-hidden transition duration-300 method-card">
                    <div class="p-6 sm:p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-3 rounded-full mr-4">
                                <i class="fas fa-laptop-code text-indigo-600 text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold text-gray-900">1. 基于 IP 的限流</h3>
                        </div>
                        <p class="text-gray-700 mb-6">
                            限制每个 IP 地址的请求数量可以有效防止某些 IP 地址对服务进行恶意攻击或过度使用资源。
                        </p>
                        
                        <div class="mb-6">
                            <h4 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                                <i class="fas fa-cog mr-2 text-indigo-500"></i>使用 Tomcat Valve 实现
                            </h4>
                            <p class="text-gray-700 mb-4">
                                Tomcat 的 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">Valve</code> 组件可以用来对请求进行过滤和控制。通过实现自定义的 Valve，可以在请求到达应用程序之前限制每个 IP 的请求数量。
                            </p>
                            
                            <div class="mb-6">
                                <h5 class="font-medium text-gray-900 mb-2">实现步骤：</h5>
                                <ol class="list-decimal pl-5 space-y-2 text-gray-700">
                                    <li>创建自定义 Valve 类</li>
                                    <li>配置 Valve 到 Tomcat 的 <code class="bg-indigo-100 text-indigo-700 px-1 py-0.5 rounded">server.xml</code> 文件中</li>
                                </ol>
                            </div>
                            
                            <div class="code-block p-4 rounded-lg mb-4">
                                <pre><code class="text-sm md:text-base">package com.demo;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.valves.ValveBase;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

public class RateLimitValve extends ValveBase {
    private static final int MAX_REQUESTS_PER_MINUTE = 100;
    private Map&lt;String, RateLimit&gt; ipRateLimits = new ConcurrentHashMap&lt;&gt;();

    @Override
    public void invoke(Request request, Response response) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request.getRequest();
        String ip = httpRequest.getRemoteAddr();
        
        RateLimit rateLimit = ipRateLimits.computeIfAbsent(ip, k -> new RateLimit(MAX_REQUESTS_PER_MINUTE));
        
        if (rateLimit.isAllowed()) {
            getNext().invoke(request, response);
        } else {
            response.sendError(HttpServletResponse.SC_TOO_MANY_REQUESTS, "Rate limit exceeded");
        }
    }
}

class RateLimit {
    private final int maxRequests;
    private final Queue&lt;Long&gt; timestamps = new LinkedList&lt;&gt;();

    public RateLimit(int maxRequests) {
        this.maxRequests = maxRequests;
    }

    public synchronized boolean isAllowed() {
        long now = System.currentTimeMillis();
        while (!timestamps.isEmpty() && timestamps.peek() < now - 60000) {
            timestamps.poll();
        }
        if (timestamps.size() < maxRequests) {
            timestamps.add(now);
            return true;
        }
        return false;
    }
}</code></pre>
                            </div>
                            
                            <div class="code-block p-4 rounded-lg">
                                <pre><code class="text-sm md:text-base">&lt;Valve className="com.demo.RateLimitValve" /&gt;</code></pre>
                            </div>
                        </div>
                        
                        <div>
                            <h4 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                                <i class="fas fa-server mr-2 text-indigo-500"></i>使用 Nginx 或其他代理服务器
                            </h4>
                            <p class="text-gray-700 mb-4">
                                在 Tomcat 前面使用 Nginx 或其他反向代理服务器可以更灵活地配置限流策略。Nginx 提供了多种限流模块，如 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">limit_req</code> 和 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">limit_conn</code>。
                            </p>
                            
                            <div class="code-block p-4 rounded-lg">
                                <pre><code class="text-sm md:text-base">http {
    limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;

    server {
        listen 80;

        location / {
            limit_req zone=perip burst=5 nodelay;
            proxy_pass http://tomcat_backend;
        }
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Method 2 -->
            <div class="grid md:grid-cols-1 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md overflow-hidden transition duration-300 method-card">
                    <div class="p-6 sm:p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-3 rounded-full mr-4">
                                <i class="fas fa-tachometer-alt text-indigo-600 text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold text-gray-900">2. 基于用户请求速率的限流</h3>
                        </div>
                        <p class="text-gray-700 mb-6">
                            对于需要更细粒度控制的场景，可以实现基于用户请求速率的限流。
                        </p>
                        
                        <div>
                            <h4 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                                <i class="fab fa-google mr-2 text-indigo-500"></i>使用 Java 的 RateLimiter
                            </h4>
                            <p class="text-gray-700 mb-4">
                                Google Guava 库提供了一个 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">RateLimiter</code> 类，用于控制请求速率。
                            </p>
                            
                            <div class="mb-6">
                                <h5 class="font-medium text-gray-900 mb-2">实现步骤：</h5>
                                <ol class="list-decimal pl-5 space-y-2 text-gray-700">
                                    <li>添加 Guava 依赖</li>
                                    <li>在 Servlet 中使用 RateLimiter</li>
                                </ol>
                            </div>
                            
                            <div class="code-block p-4 rounded-lg mb-4">
                                <pre><code class="text-sm md:text-base">&lt;dependency&gt;
    &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
    &lt;artifactId&gt;guava&lt;/artifactId&gt;
    &lt;version&gt;30.1-jre&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
                            </div>
                            
                            <div class="code-block p-4 rounded-lg">
                                <pre><code class="text-sm md:text-base">import com.google.common.util.concurrent.RateLimiter;

public class RateLimitedServlet extends HttpServlet {
    private static final RateLimiter rateLimiter = RateLimiter.create(100); // 每秒 100 次请求

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (rateLimiter.tryAcquire()) {
            // 处理请求
        } else {
            resp.sendError(HttpServletResponse.SC_TOO_MANY_REQUESTS, "Rate limit exceeded");
        }
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Method 3 -->
            <div class="grid md:grid-cols-1 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden transition duration-300 method-card">
                    <div class="p-6 sm:p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-3 rounded-full mr-4">
                                <i class="fas fa-weight text-indigo-600 text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold text-gray-900">3. 基于请求体大小的流量控制</h3>
                        </div>
                        <p class="text-gray-700 mb-6">
                            控制请求体的大小可以防止过大的请求占用过多的资源。
                        </p>
                        
                        <div class="grid md:grid-cols-2 gap-6">
                            <div>
                                <h4 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                                    <i class="fas fa-cog mr-2 text-indigo-500"></i>使用 Tomcat 配置
                                </h4>
                                <p class="text-gray-700 mb-4">
                                    Tomcat 允许在 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">server.xml</code> 或 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">context.xml</code> 中配置请求体的最大大小。
                                </p>
                                
                                <div class="code-block p-4 rounded-lg">
                                    <pre><code class="text-sm md:text-base">&lt;Connector port="8080" 
           maxPostSize="1048576" /&gt; &lt;!-- 限制请求体最大为 1MB --&gt;</code></pre>
                                </div>
                            </div>
                            
                            <div>
                                <h4 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                                    <i class="fas fa-server mr-2 text-indigo-500"></i>使用 Nginx
                                </h4>
                                <p class="text-gray-700 mb-4">
                                    Nginx 也可以限制请求体的大小，防止过大的请求进入 Tomcat。
                                </p>
                                
                                <div class="code-block p-4 rounded-lg">
                                    <pre><code class="text-sm md:text-base">server {
    listen 80;

    location / {
        client_max_body_size 1m;  # 限制请求体最大为 1MB
        proxy_pass http://tomcat_backend;
    }
}</code></pre>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section id="visualization" class="mb-16">
            <h2 class="text-3xl font-bold text-gray-900 mb-8 flex items-center">
                <i class="fas fa-project-diagram mr-3 text-indigo-600"></i>方案对比与选择指南
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6 sm:p-8">
                <div class="mermaid">
                    graph TD
                    A[Tomcat限流方案] --> B[基于IP的限流]
                    A --> C[基于请求速率的限流]
                    A --> D[基于请求体大小的控制]
                    
                    B --> B1[Tomcat Valve实现]
                    B --> B2[Nginx限流模块]
                    
                    C --> C1[Guava RateLimiter]
                    
                    D --> D1[Tomcat配置]
                    D --> D2[Nginx配置]
                    
                    style A fill:#4f46e5,color:#fff
                    style B fill:#7c3aed,color:#fff
                    style C fill:#7c3aed,color:#fff
                    style D fill:#7c3aed,color:#fff
                    style B1 fill:#8b5cf6,color:#fff
                    style B2 fill:#8b5cf6,color:#fff
                    style C1 fill:#8b5cf6,color:#fff
                    style D1 fill:#8b5cf6,color:#fff
                    style D2 fill:#8b5cf6,color:#fff
                </div>
                
                <div class="mt-8 grid md:grid-cols-3 gap-6">
                    <div class="border-l-4 border-indigo-500 pl-4">
                        <h3 class="text-xl font-semibold mb-2">适用场景</h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-check-circle text-indigo-500 mr-2"></i>防止DDoS攻击</li>
                            <li><i class="fas fa-check-circle text-indigo-500 mr-2"></i>API接口防护</li>
                            <li><i class="fas fa-check-circle text-indigo-500 mr-2"></i>高并发系统</li>
                        </ul>
                    </div>
                    
                    <div class="border-l-4 border-indigo-500 pl-4">
                        <h3 class="text-xl font-semibold mb-2">方案优势</h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-star text-indigo-500 mr-2"></i>实现简单</li>
                            <li><i class="fas fa-star text-indigo-500 mr-2"></i>性能影响小</li>
                            <li><i class="fas fa-star text-indigo-500 mr-2"></i>配置灵活</li>
                        </ul>
                    </div>
                    
                    <div class="border-l-4 border-indigo-500 pl-4">
                        <h3 class="text-xl font-semibold mb-2">选择建议</h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-lightbulb text-indigo-500 mr-2"></i>结合使用多层防护</li>
                            <li><i class="fas fa-lightbulb text-indigo-500 mr-2"></i>根据业务特点选择</li>
                            <li><i class="fas fa-lightbulb text-indigo-500 mr-2"></i>考虑性能开销</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-16">
            <div class="bg-indigo-50 rounded-xl p-8 sm:p-10">
                <h2 class="text-3xl font-bold text-gray-900 mb-6">最佳实践总结</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                            <i class="fas fa-check-circle mr-2"></i>实施建议
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                                <span>对于面向公众的服务，建议在Nginx层先做基础限流</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                                <span>关键业务接口建议在应用层增加更细粒度的限流控制</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                                <span>文件上传等场景务必配置请求体大小限制</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                            <i class="fas fa-exclamation-triangle mr-2"></i>注意事项
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                                <span>合理设置限流阈值，避免影响正常用户</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                                <span>考虑实现优雅降级策略</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                                <span>监控限流触发的频率，及时调整策略</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold text-white">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="mt-8 pt-8 border-t border-gray-800 text-center text-sm text-gray-500">
                <p>© 2023 技术小馆. 版权所有.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```